package com.navi.controller;

import com.navi.entity.HisConfigure;
import com.navi.util.JacksonUtil;
import com.rabbitmq.http.client.Client;
import com.rabbitmq.http.client.domain.QueueInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by admin on 2017/9/18.
 */
@RestController
public class ShowStatusController {
    public static final String HISCONFIG_KEY = "hisconfig";
    //    @Autowired
//    private ConnectionFactory connectionFactory;

    @Value("${spring.rabbitmq.host}")
    private String rabbitMQHost;
    @Value("${spring.rabbitmq.username}")
    private String rabbitMQUser;
    @Value("${spring.rabbitmq.password}")
    private String rabbitMQPassword;
    @Value("${spring.rabbitmq.http.port}")
    private String rabbitMQHttpPort;
    @Value("${spring.rabbitmq.queues}")
    private String queueNames;
    @Value("${spring.rabbitmq.exchange}")
    private String exchange;


    @Autowired
    private JedisPool jedisPool;

    @RequestMapping(value = "/showStatus.do")
    public List<QueueInfo> showAllStatus() {
        Client client = null;
        try {
//
            String url = String.format("http://%s:%s/api/bindings", rabbitMQHost, rabbitMQHttpPort);
            client = new Client(url, rabbitMQUser, rabbitMQPassword);


            final List<String> queueStatusEntityList = client.getBindings().stream().filter(bind ->
                    StringUtils.equalsIgnoreCase(bind.getSource(), exchange)
            ).map(bind -> bind.getDestination()).collect(Collectors.toList());

            url = String.format("http://%s:%s/api/queues", rabbitMQHost, rabbitMQHttpPort);
            client = new Client(url, rabbitMQUser, rabbitMQPassword);

            final List<QueueInfo> queueInfoList = client.getQueues().stream().filter(queueInfo ->
                queueStatusEntityList.parallelStream().anyMatch(
                        qName ->
                                StringUtils.equalsIgnoreCase(qName, queueInfo.getName())
                )
            ).collect(Collectors.toList());

            return queueInfoList;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        return null;

//        final List<QueueInfo> queues = client.getQueues().parallelStream().filter(
//                queueInfo -> queueInfo.getsou
//                queue ->
//                        Arrays.stream(queueNames.split(",")).anyMatch(
//                                queueInConfig -> StringUtils.equalsIgnoreCase(queueInConfig, queue.getName())
//                        )
//        ).collect(Collectors.toList());
//        return queues;
    }

    @RequestMapping(value = "addConfig.do")
    public HisConfigure addConfig(HisConfigure hisConfigure) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.hset(HISCONFIG_KEY, hisConfigure.getTableName(), JacksonUtil.toJSONStr(hisConfigure));
            return hisConfigure;
        }
    }

    @RequestMapping(value = "listConfig.do")
    public List<HisConfigure> listConfig() {
        try (Jedis jedis = jedisPool.getResource()) {
            final Map<String, String> stringStringMap = jedis.hgetAll(HISCONFIG_KEY);

            return stringStringMap.entrySet().stream()
                    .map(Map.Entry::getValue)
                    .map(jsonStr -> (HisConfigure) JacksonUtil.fromJson(jsonStr, HisConfigure.class))
                    .collect(Collectors.toList());
        }
    }

    @RequestMapping(value = "getConfig.do")
    public HisConfigure getConfig(String tableName) {
        try (Jedis jedis = jedisPool.getResource()) {
            String jsonStr = jedis.hget(HISCONFIG_KEY, tableName);
            return (HisConfigure) JacksonUtil.fromJson(jsonStr, HisConfigure.class);
        }
    }


    @RequestMapping(value = "deleteConfig.do")
    public String delConfig(String tableName) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.hdel(HISCONFIG_KEY, tableName);
            return "success";
        }
    }

}
